<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: IndexController.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_InterestsController extends Core_Controller_Action_Standard {
	public function init() {
		$this->view->nophoto = Engine_Api::_ ()->like ()->getNoPhotos ();
		
		if ($this->_getParam ( 'action' ) == 'suggest') {
			return;
		}
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			// Can specifiy custom id
			$id = $this->_getParam ( 'id', null );
			$subject = null;
			if (null === $id) {
				$subject = $this->_helper->api ()->user ()->getViewer ();
				$this->_helper->api ()->core ()->setSubject ( $subject );
			} else {
				$subject = $this->_helper->api ()->user ()->getUser ( $id );
				$this->_helper->api ()->core ()->setSubject ( $subject );
			}
		}
		
		if (! empty ( $id )) {
			$params = array ('params' => array ('id' => $id ) );
		} else {
			$params = array ();
		}
		
		// Set up navigation
		$this->view->navigation = $navigation = $this->_helper->api ()->getApi ( 'menus', 'core' )->getNavigation ( 'user_edit', array ('params' => array ('id' => $id ) ), 'user_edit_interests' );
		
		// Set up require's
		$this->_helper->requireUser ();
		$this->_helper->requireSubject ( 'user' );
		
		$this->view->viewer = Engine_Api::_ ()->user ()->getViewer ();
		
		$this->view->labels = Engine_Api::_ ()->like ()->getInterestTypes ();
		$this->view->icons = Engine_Api::_ ()->like ()->getInterestIcons ();
		
		$this->view->moduleApi = $moduleApi = Engine_Api::_ ()->getDbTable ( 'modules', 'core' );
	}
	
	public function indexAction() {
		$viewer = Engine_Api::_ ()->user ()->getViewer ();
		$api = Engine_Api::_ ()->like ();
		$user_id = ( int ) $viewer->getIdentity ();
		
		$interests = array_keys ( $this->view->labels );
		$table = $api->getTable ();
		
		$manifest = Zend_Registry::get ( 'Engine_Manifest' );
		$itemTypes = array ();
		foreach ( $manifest as $man ) {
			if (! isset ( $man ['items'] )) {
				continue;
			}
			foreach ( $man ['items'] as $item ) {
				$itemTypes [] = $item;
			}
		}
		$interests = array_intersect ( $interests, $itemTypes );
		
		$availableLabels = array ('everyone' => 'Everyone', 'registered' => 'All Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		$viewOptions = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'user', $viewer, 'auth_interest' );
		$this->view->viewOptions = $viewOptions = array_intersect_key ( $availableLabels, array_flip ( $viewOptions ) );
		
		$auth = Engine_Api::_ ()->authorization ()->context;
		$roles = array ('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone' );
		
		if ($this->getRequest ()->isPost ()) {
			$data = $this->_getParam ( 'data' );
			$view_interest = $this->_getParam ( 'view_interest' );
			
			$modules = array_keys ( $data );
			$where = implode ( '","', $modules );
			
			$db = $table->getAdapter ();
			$db->beginTransaction ();
			
			try {
				if (empty ( $view_interest )) {
					$view_interest = 'everyone';
				}
				$viewMax = array_search ( $view_interest, $roles );
				foreach ( $roles as $i => $role ) {
					$auth->setAllowed ( $viewer, $role, 'interest', ($i <= $viewMax) );
				}
				$table->delete ( 'poster_type = "user" AND poster_id = ' . $user_id . ' AND resource_type IN ("' . $where . '")' );
				foreach ( $data as $key => $value ) {
					$ids = explode ( ',', $value );
					if (! empty ( $ids )) {
						foreach ( $ids as $id ) {
							$id = ( int ) $id;
							if (! $id) {
								continue;
							}
							$toLikeData = array ('resource_type' => $key, 'resource_id' => $id, 'poster_id' => $user_id, 'poster_type' => 'user' );
							$table->insert ( $toLikeData );
						}
					}
				}
				$db->commit ();
			} catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->view->message = "like_Your changes has been saved.";
		}
		
		foreach ( $roles as $role ) {
			if ($auth->isAllowed ( $viewer, $role, 'interest' )) {
				$this->view->privacyValue = $role;
			}
		}
		
		$params = array ('poster_type' => $viewer->getType (), 'poster_id' => $user_id, 'fetch' => 'resource' );
		$select = $api->getLikesSelect ( $params );
		$in = implode ( '","', $interests );
		$select->where ( 'like1.resource_type IN ("' . $in . '")' )->where ( 'like1.resource_id <> 0' );
		$rawData = $api->getTable ()->fetchAll ( $select );
		
		if (count ( $rawData ) > 0) {
			$items = array ();
			foreach ( $rawData as $data ) {
				$item = Engine_Api::_ ()->getItem ( $data->resource_type, $data->resource_id );
				if ($item) {
					$items [$data->resource_type] [] = $item;
				}
			}
			$this->view->items = $items;
		} else {
			$this->view->items = array ();
		}
		
		$data = Engine_Api::_ ()->like ()->getMatches ( $viewer );
		
		$this->view->subject = $viewer;
		$this->view->matches = $data ['paginator'];
		$this->view->counts = $data ['counts'];
		$this->view->matches->setItemCountPerPage ( 16 );
		
		$this->view->paginators = array ();
		$this->view->counts = array ();
		
		foreach ( $interests as $interest ) {
			$data = $api->getMostLiked ( $interest, 3 );
			if ($data) {
				$this->view->paginators [$interest] = $data ['paginator'];
				$this->view->counts [$interest] = $data ['counts'];
			}
		}
	}
	
	public function suggestAction() {
		$type = $this->_getParam ( 'type' );
		$text = $this->_getParam ( 'text' );
		$except = $this->_getParam ( 'except' );
		$viewer = $this->view->viewer;
		
		$table = Engine_Api::_ ()->getItemTable ( $type );
		$primary = $table->info ( 'primary' );
		
		$select = $table->select ()->where ( 'title LIKE "%' . $text . '%"' )->limit ( 10 );
		
		if (! empty ( $except )) {
			$except = implode ( ',', $except );
			$select->where ( $primary [1] . " NOT IN (" . $except . ")" );
		}
		
		$this->_helper->layout->disableLayout ();
		$items = $table->fetchAll ( $select );
		$data = array ();
		
		$nophoto = $this->view->nophoto;
		$types = array ('page', 'event', 'group' );
		
		foreach ( $items as $item ) {
			if (! $item->authorization ()->isAllowed ( $viewer, 'view' )) {
				continue;
			}
			
			if (in_array ( $item->getType (), $types ) && ! Engine_Api::_ ()->like ()->isAllowed ( $item )) {
				continue;
			}
			
			$img = $item->getPhotoUrl ( 'thumb.icon' );
			if (! $img) {
				$img = $this->view->baseUrl () . $nophoto [$item->getType ()];
			}
			
			$foo = array ('id' => $item->getGuid (), 'label' => $item->getTitle (), 'img' => $img, 'href' => $item->getHref () );
			$data [] = $foo;
		}
		
		return $this->_helper->json ( $data );
	}
}